package com.yc.cloud.activiti.api;

import com.yc.cloud.common.model.response.Outcome;
import com.yc.cloud.activiti.utils.ActivitiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨智杰
 * @since 2021/11/24 14:36
 */
@RestController
@Api(tags = "工作流相关接口")
@RequestMapping("/activiti")
public class ActivitiApi {

    @Resource
    private ActivitiUtil activitiUtil;

    @GetMapping("/test")
    @ApiOperation("部署流程测试")
    public Outcome test() {
        activitiUtil.deployAProcess();
        return Outcome.success();
    }

    @GetMapping("/start")
    @ApiOperation("流程开始")
    public Outcome start(String key) {
        return Outcome.success().setData(activitiUtil.start(key).getId());
    }

    @GetMapping("/complete")
    @ApiOperation("推进任务")
    public Outcome complete(String id) {
        activitiUtil.completeTask(id);
        return Outcome.success();
    }

    @GetMapping("/getAllTask")
    @ApiOperation("查询所有任务")
    public Outcome<List<String>> getAllTask() {
        List<Task> tasks = activitiUtil.queryAllTask();
        List<String> ids = tasks.stream().map(Task::getId).collect(Collectors.toList());
        return Outcome.success(ids);
    }


    @GetMapping("/getTaskInfo")
    @ApiOperation("查询流程示例进度")
    public Outcome getTaskInfo(String id) {
        return Outcome.success(activitiUtil.queryExecution(id).getName());
    }

    @GetMapping("/isEnd")
    @ApiOperation("查询当前任务是否已经结束")
    public Outcome isEnd(String id){
        return Outcome.success().setData(activitiUtil.isEnd(id));
    }

}
